package security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import security.dao.UserDao;
import security.entity.UserDto;

@Service
public class SpringUserDetailService implements UserDetailsService {

    @Autowired
    private UserDao userDao;
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {

        System.out.println("username =" + s);

        UserDto userDto = userDao.selectOne(s);
        if (userDto != null){
            UserDetails de = User.withUsername(userDto.getUsername()).password(userDto.getPassword()).authorities("p1").build();
            return de;
        }else{
            throw new RuntimeException("账号不存在");
        }
    }
}
